package com.humanresource.service.impl.clerkrecord;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.humanresource.constant.Constant;
import com.humanresource.dao.mapper.clerkrecord.ClerkbaseinfoMapper;
import com.humanresource.dao.mapper.clerkrecord.ClerkinstposMapper;
import com.humanresource.dao.mapper.clerkrecord.ClerkrecordsMapper;
import com.humanresource.dao.mapper.clerkrecord.InstMapper;
import com.humanresource.dao.mapper.clerkrecord.PosMapper;
import com.humanresource.dao.mapper.clerksalary.ClerkrewardpunishstandardMapper;
import com.humanresource.domain.po.clerkrecord.Clerkbaseinfo;
import com.humanresource.domain.po.clerkrecord.ClerkbaseinfoExample;
import com.humanresource.domain.po.clerkrecord.Clerkinstpos;
import com.humanresource.domain.po.clerkrecord.ClerkinstposExample;
import com.humanresource.domain.po.clerkrecord.Clerkrecords;
import com.humanresource.domain.po.clerkrecord.ClerkrecordsExample;
import com.humanresource.domain.po.clerkrecord.Inst;
import com.humanresource.domain.po.clerkrecord.InstExample;
import com.humanresource.domain.po.clerkrecord.Pos;
import com.humanresource.domain.po.clerkrecord.PosExample;
import com.humanresource.domain.po.clerksalary.Clerkrewardpunishstandard;
import com.humanresource.service.clerkrecord.ClerkBaseInfoService;
import com.humanresource.utils.ValidateUtil;

@Service
public class ClerkBaseInfoServiceImpl implements ClerkBaseInfoService {

	@Autowired
	private ClerkbaseinfoMapper clerkbaseinfoMapper;

	@Autowired
	private ClerkinstposMapper clerkinstposMapper;

	@Autowired
	private InstMapper instMapper;

	@Autowired
	private PosMapper posMapper;

	@Autowired
	ClerkrewardpunishstandardMapper clerkrewardpunishstandardMapper;

	@Autowired
	private ClerkrecordsMapper clerkrecordsMapper;

	@Override
	public void insert(Clerkbaseinfo clerkbaseinfo) {
		this.clerkbaseinfoMapper.insert(clerkbaseinfo);
	}

	@Override
	public Clerkbaseinfo login(String clerkname, String clerkpassword) {
		ClerkbaseinfoExample clerkbaseinfoExample = new ClerkbaseinfoExample();
		ClerkbaseinfoExample.Criteria criteria = clerkbaseinfoExample
				.createCriteria();
		criteria.andClerknameEqualTo(clerkname);
		criteria.andClerkpasswordEqualTo(clerkpassword);
		List<Clerkbaseinfo> clerkbaseinfos = this.clerkbaseinfoMapper
				.selectByExample(clerkbaseinfoExample);
		return (clerkbaseinfos.size() > 0 ? clerkbaseinfos.get(0) : null);
	}

	@Override
	public Clerkbaseinfo findById(Integer clerkId) {
		return this.clerkbaseinfoMapper.selectByPrimaryKey(clerkId);
	}

	@Override
	public Clerkbaseinfo findByName(String name) {
		ClerkbaseinfoExample clerkbaseinfoExample = new ClerkbaseinfoExample();
		ClerkbaseinfoExample.Criteria criteria = clerkbaseinfoExample
				.createCriteria();
		criteria.andClerknameEqualTo(name);
		return this.clerkbaseinfoMapper.selectByExample(clerkbaseinfoExample)
				.get(0);
	}

	@Override
	public void addPosToClerk(Clerkinstpos clerkinstpos) {
		this.clerkinstposMapper.insertSelective(clerkinstpos);
	}

	@Override
	public Clerkinstpos findByClerkId(Integer ClerkId) {
		return this.clerkinstposMapper.selectByPrimaryKey(ClerkId);
	}

	@Override
	public void update(Clerkbaseinfo clerkbaseinfo) {
		this.clerkbaseinfoMapper.updateByPrimaryKeySelective(clerkbaseinfo);
	}

	@Override
	public List<Clerkinstpos> findBy3instname2posname(String thirdinstname,
			String posname) {
		// 通过机构名thirdinstname查找机构编号instid
		InstExample instExample = new InstExample();
		InstExample.Criteria criteriainst = instExample.createCriteria();
		criteriainst.andInstnameEqualTo(thirdinstname);
		List<Inst> insts = this.instMapper.selectByExample(instExample);
		// 获取所有的机构id
		List<Integer> instidList = new ArrayList<Integer>();
		for (Inst inst : insts) {
			instidList.add(inst.getInstid());
		}

		// 通过职位名称posname获得二级职位
		PosExample posExample = new PosExample();
		PosExample.Criteria criteriapos = posExample.createCriteria();
		criteriapos.andPosnameEqualTo(posname);
		Pos pos = this.posMapper.selectByExample(posExample).get(0);

		// 通过二级职位查找所有的职称
		PosExample posExample2 = new PosExample();
		PosExample.Criteria criteria2pos = posExample2.createCriteria();
		criteria2pos.andPospidEqualTo(pos.getPosid());
		List<Pos> listpos = this.posMapper.selectByExample(posExample2);
		// 获取所有的三级职称id
		List<Integer> posidList = new ArrayList<Integer>();
		for (Pos pos2 : listpos) {
			posidList.add(pos2.getPosid());
		}

		ClerkinstposExample clerkinstposExample = new ClerkinstposExample();
		ClerkinstposExample.Criteria criteria = clerkinstposExample
				.createCriteria();
		criteria.andInstidIn(instidList);
		criteria.andPosidIn(posidList);
		List<Clerkinstpos> clerkinstposList = this.clerkinstposMapper
				.selectByExample(clerkinstposExample);
		return clerkinstposList.size() > 0 ? clerkinstposList : null;
	}

	@Override
	public List<Clerkrecords> findBy3instname2posnameandcretetime(
			String thirdinstname, String posname, String starttime,
			String endtime) {
		List<Clerkinstpos> clerkinstpos = findBy3instname2posname(
				thirdinstname, posname);

		if (clerkinstpos == null) {
			return null;
		}
		// 获得所有的员工编号
		List<Integer> clerkidList = new ArrayList<Integer>();
		for (Clerkinstpos clerkinstpos2 : clerkinstpos) {
			clerkidList.add(clerkinstpos2.getClerkid());
		}

		// 根据条件查询所有的clerkrecords
		ClerkrecordsExample clerkrecordsExample = new ClerkrecordsExample();
		ClerkrecordsExample.Criteria criteria = clerkrecordsExample
				.createCriteria();
		criteria.andClerkidIn(clerkidList);
		if ((!ValidateUtil.isNull(starttime))
				&& (!ValidateUtil.isNull(endtime))) {
			criteria.andCreatetimeBetween(starttime, endtime);
		}
		criteria.andIsdeleteNotEqualTo(Constant.DELETE);
		return this.clerkrecordsMapper.selectByExample(clerkrecordsExample);
	}

	@Override
	public List<Clerkbaseinfo> clerknameLike(String clerkname) {
		ClerkbaseinfoExample clerkbaseinfoExample = new ClerkbaseinfoExample();
		ClerkbaseinfoExample.Criteria criteria = clerkbaseinfoExample
				.createCriteria();
		criteria.andClerknameLike("%" + clerkname + "%");
		return this.clerkbaseinfoMapper.selectByExample(clerkbaseinfoExample);
	}

	@Override
	public List<Clerkinstpos> findByInstId(Integer instid) {
		ClerkinstposExample clerkinstposExample = new ClerkinstposExample();
		ClerkinstposExample.Criteria criteria = clerkinstposExample
				.createCriteria();
		criteria.andInstidEqualTo(instid);
		return this.clerkinstposMapper.selectByExample(clerkinstposExample);
	}

	@Override
	public void setClerkrewardpunishstandard(
			Clerkrewardpunishstandard clerkrewardpunishstandard) {
		this.clerkrewardpunishstandardMapper
				.insertSelective(clerkrewardpunishstandard);
	}
}
